﻿@page "/FeedingPage"
@inject PickingServer pickingServer
@using System.ComponentModel
@using AntDesign.TableModels
@using ShanjinPDAUI.ModelEntity
@inject ModalService _modalService
@inject MessageService _message
<h3>主机上料</h3>
<div>
    <Row>
        <Col Span="24">请选择主机</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@Machines" 
                AllowClear="true"
                ValueOnClear="@(0)"
                @bind-Value="@Machineselet"
                ValueName="@nameof(Machine_Entity.Devid)"
                LabelName="@nameof(Machine_Entity.Remark)">
        </Select>
        </Col>
    </Row>
    <Row>
        <Col Span="24">
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@CX">查询缓存架</Button>
        </Col>
    </Row>
    <Row>
        <Col Span="24">请选择缓存架(缓存架上料时需要选择)</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@FullCaches"
                AllowClear="true"
                ValueOnClear="@(0)"
                @bind-Value="@chaneselet"
                ValueName="@nameof(FullCache_Entity.Deviceid)"
                LabelName="@nameof(FullCache_Entity.Remark)">
        </Select>
        </Col>
    </Row>
    <br>
    <Row>
        <Col Span="24">
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat">上料</Button>
        </Col>
    </Row>
    <br>

  
</div>

@code {
    ObservableCollection<Machine_Entity> Machines { get; set; } = new ObservableCollection<Machine_Entity>();
    ObservableCollection<FullCache_Entity> FullCaches { get; set; } = new ObservableCollection<FullCache_Entity>();
    int Machineselet { get; set; } = 0;
    int chaneselet { get; set; } = 0;
    protected override async Task OnInitializedAsync()
    {
        var machines = await pickingServer.GetMachine();
        if (machines != null)
        {
            Machines.Clear();
            foreach (var item in machines)
            {
                Machines.Add(item);
            }
        }

        base.OnInitialized();
    }
    private async Task Creat()
    {
        _modalService.Confirm(new ConfirmOptions()
            {
                Title = "提示",
                Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
            Content = "是否提交？",
            OkText = "确认",
            OnOk = async (e) =>
            {

               
                if (chaneselet != 0)
                {
                    var Machine = Machines.FirstOrDefault(x => x.Devid == Machineselet);
                    if (Machine != null)
                    {
                        var cache = FullCaches.FirstOrDefault(x => x.Deviceid == chaneselet);
                        if (cache!=null)
                       {
                            var b = await pickingServer.Feeding(Machine, cache.MESName, cache.Type, cache.Code, cache.Lot);
                            await _message.Info(b);
                        }
                        else
                        {
                            await _message.Info("不满足上料条件");
                        }
                      

                    }
                }
                else
                {
                    var Machine = Machines.FirstOrDefault(x => x.Devid == Machineselet);
                    if (Machine != null)
                    {
                        var agvs = await pickingServer.GetAGVs();
                        var agv = agvs.FirstOrDefault(x => x.AreaType == Machine.PutAre);
                        if (agv != null)
                        {
                            var b = await pickingServer.Feeding(Machine, agv.MESName, agv.Type, agv.Code, agv.Lot);
                            await _message.Info(b);
                        }
                        else
                        {
                            await _message.Info("不满足上料条件");
                        }


                    }
                  

                }


            },
            CancelText = "取消"
        });


    }

    private async Task CX()
    {
        chaneselet = 0;
        var funcache = await pickingServer.GetCaches(Machineselet);
        if (funcache != null)
        {
            FullCaches.Clear();
            foreach (var item in funcache)
            {
                FullCaches.Add(item);
            }
        }
   
        this.StateHasChanged();
    }
}

